import Vue from 'vue'
import App from './App'
import router from './router/index'
// import store from './store/store'
import store from './store'
import axios from 'axios'
import Qs from 'qs'
import iView from 'iview'
// import 'iview/dist/styles/iview.css'
import './assets/style/style.less'
import '../../my-theme/dist-theme/iview.css'
import './http'
import './assets/style/reset.css'
/*var axios_instance = axios.create({
    baseURL: '',
    transformRequest: [function (data) {
        data = Qs.stringify(data);
        return data;
    }]
})*/
// 添加请求拦截器
/*axios_instance.interceptors.request.use(
    config => {
        const token = sessionStorage.getItem('accessToken');
        if (token) {
            config.headers.authorization = token;
            /!*config.headers.authorization = 'aaa';*!/
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
);*/

// 添加响应拦截器
/*axios_instance.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        if (error.response.status == '401') {  //过期失效
            router.replace({
                path: '/login',
                // param: {redirect: router.currentRoute.fullPath}
            })
        }
        return Promise.reject(error);
    }
);*/

Vue.use(iView)
Vue.config.debug = true
Vue.config.productionTip = false
/*Vue.prototype.$http = Vue.http = axios_instance*/
Vue.prototype.$http = Vue.http = axios
import moment from 'moment'
Vue.prototype.$moment = Vue.moment = moment
//Vue.http.options.root = 'http://192.168.3.150'

//Vue.http.options.emulateJSON = true

/**
 * 跳转逻辑
 * 登录成功后 新用户需要修改密码直接跳到密码页面，修改完之后跳到登录页成功登录才允许跳转其他页面
 */

router.beforeEach(function (to, from, next) {
    let auth = localStorage.getItem('accessToken');
    // console.log(from.path, to.path,store.state.login.userInfo.needModifyPWD)
    // 修改密码
    if (store.state.login.userInfo.needModifyPWD && (from.path.indexOf('/password') > -1) && (to.path!=='/login')) {
        next(false)
    } else if (from.path!=='/'&&from.path!=='/login'&&to.meta.code&&!store.getters['login/hasMenuPermission'](to.meta.code.substr(0,7), 7)){
        next(false)
    } else if (to.meta.requireAuth && !auth) {
        next('/login');
    } else {
        if (from.path!=='/'&&from.path!=='/login'&&to.path!=='/'&&to.path!=='/login') {
            sessionStorage.setItem('myPrevPath', to.path)
            store.commit('login/setCurrentPath', to.path)
        }
        next();
    }
})

new Vue({
  el: '#app',
  router: router,
  store: store,
  template: '<App/>',
  components: { App }
})
